﻿using System;
using System.Linq;
using Missbot.Service;
using MongoDB.Bson.Serialization.Attributes;

namespace Missbot.AIModule
{
    public class PicReviewRecord : DbBaseEntity
    {
        public string UserID { get; init; }

        public string UserName { get; init; }

        public string GroupID { get; set; }

        public string GroupName { get; set; }

        public string Usage { get; init; }

        public PicReviewStatus Status { get; set; } = PicReviewStatus.Waiting;

        [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime CreateTime { get; set; } = DateTime.Now;

        [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime? ReviewTime { get; set; }

        public string FileID { get; set; }

        public string Channel { get; set; }
    }

    public static class PicReviewDA
    {
        public static PicReviewRecord GetWaiting(string UserID, string Usage)
        {
            return MongoService<PicReviewRecord>.GetOnly(p => p.UserID == UserID && p.Usage == Usage && p.Status == PicReviewStatus.Waiting);
        }

        public static PicReviewRecord GetLastestWaiting()
        {
            return MongoService<PicReviewRecord>.Get(p => p.Status == PicReviewStatus.Waiting).OrderBy(p => p.CreateTime).FirstOrDefault();
        }
        
        public static void Update(this PicReviewRecord entity)
        {
            MongoService<PicReviewRecord>.Update(entity);
        }

        public static void Insert(this PicReviewRecord entity)
        {
            MongoService<PicReviewRecord>.Insert(entity);
        }

        public static long CountWaiting()
        {
            return MongoService<PicReviewRecord>.Count(p => p.Status == PicReviewStatus.Waiting);
        }
    }

    public enum PicReviewStatus
    {
        Waiting,
        Passed,
        Refused
    }
}